package com.baifendian.common.task;

import com.baifendian.common.utils.DateUtils;
import com.baifendian.dao.entity.RcOverviewStat;
import com.baifendian.dao.mapper.RcOverviewStatMapper;
import com.baifendian.service.ExcApiTrackService;
import java.util.Date;
import java.util.concurrent.Callable;
import java.util.concurrent.atomic.AtomicInteger;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * @author peng.guo on 2018/3/5.
 */
public class APITask implements Callable<String>,Runnable {
  private String name;
  private final AtomicInteger seq = new AtomicInteger(0);

//  private ThreadLocal<ExcApiTrackService> excApiTrackService  = new ThreadLocal<ExcApiTrackService>();
  private ExcApiTrackService excApiTrackService;
public APITask(String name) {
  this.name = name;
}

  public APITask(ExcApiTrackService excApiTrackService,String name) {
    this.name = name;
//    this.excApiTrackService.set(excApiTrackService);
    this.excApiTrackService = excApiTrackService;
  }

  @Override
  public String call() throws Exception {

    int totalApi = excApiTrackService.getTotalApi();
    int totalApiByDay = excApiTrackService.getTotalApiByDay(new Date());

    try {
      Thread.sleep(1000);
    } catch (InterruptedException e) {
      e.printStackTrace();
    }

    System.out.println(name + "" + seq.incrementAndGet()+ " || " + totalApi + "||" + totalApiByDay);
    return null;

  }

    @Override
  public void run() {
//      int totalApi = excApiTrackService.get().getTotalApi();
//      Date date = DateUtils.stringToDate("02/22/2018");
      Date date = DateUtils.localDateTimeToDate(DateUtils.getCurrentTime());
//      RcOverviewStat rcOverviewStat = new RcOverviewStat();
      int totalApi = excApiTrackService.getTotalApi();
      int totalApiByDay = excApiTrackService.getTotalApiByDay(date);
      int totalAudit = excApiTrackService.getTotalAudit();
//      int totalAuditByDay = excApiTrackService.getTotalAuditByDay(DateUtils.stringToDate("03/25/2018"));
      int totalAuditByDay = excApiTrackService.getTotalAuditByDay(date);

      int institution = excApiTrackService.getInstitution();
      int system = excApiTrackService.getSystem();
      int db = excApiTrackService.getDbNumber();
      int ftp = excApiTrackService.getFtpNumber();

      RcOverviewStat ros = new RcOverviewStat();
      ros.setNumApiInvokeTotal(totalApi);
      ros.setNumApiInvokeToday(totalApiByDay);
      ros.setNumDataExcTotal(totalAudit);
      ros.setNumDataExcToday(totalAuditByDay);
      ros.setNumInstitution(institution);
      ros.setNumSystem(system);
      ros.setNumServer(db+ftp);
      ros.setStatDt(DateUtils.localDateTimeToDate(DateUtils.getCurrentTime()));
      excApiTrackService.delRcOverviewStat();
      excApiTrackService.saveRcOverviewStat(ros);
//      try {
//        Thread.sleep(1000);
//      } catch (InterruptedException e) {
//        e.printStackTrace();
//      }

//      System.out.println(name + "" + seq.incrementAndGet()+ " || " + totalApi + "||" + totalApiByDay);
//      System.out.println("" + totalAudit + " || " + totalAuditByDay + "||");
//      System.out.println("" + institution + " || " + system + "||" +(db + ftp));
    }
}
